package pl.koziolekweb.jqr.ui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SpringLayout;

public class MainScreen extends JFrame {

	private static final long serialVersionUID = 1L;
	private JMenuBar menuBar;

	private WordPanel wordPanel;

	private ControlWidget controlWidget;

	private TextProcessor textProcessor;

	public static MainScreen build() {
		MainScreen instance = new MainScreen();
		instance.buildBasics();
		instance.buildMenu();
		instance.buildElements();
		instance.useLayout();
		instance.bind();
		return instance;
	}

	private void bind() {
		textProcessor = new TextProcessor(wordPanel);
		controlWidget.setTextProcessor(textProcessor);
	}

	private void useLayout() {
		SpringLayout layout = new SpringLayout();
		layout.putConstraint(SpringLayout.WEST, wordPanel, 5,
				SpringLayout.WEST, getContentPane());
		layout.putConstraint(SpringLayout.NORTH, wordPanel, 5,
				SpringLayout.NORTH, getContentPane());
		layout.putConstraint(SpringLayout.WEST, controlWidget, 5,
				SpringLayout.WEST, getContentPane());
		layout.putConstraint(SpringLayout.NORTH, controlWidget, 5,
				SpringLayout.SOUTH, wordPanel);
		layout.putConstraint(SpringLayout.EAST, getContentPane(), 5,
				SpringLayout.EAST, controlWidget);
		layout.putConstraint(SpringLayout.SOUTH, getContentPane(), 5,
				SpringLayout.SOUTH, controlWidget);
		setLayout(layout);
		pack();
	}

	private void buildElements() {
		wordPanel = new WordPanel();
		controlWidget = new ControlWidget(textProcessor);
		add(wordPanel);
		add(controlWidget);
	}

	private MainScreen() {
	}

	private void buildBasics() {
		setTitle("Java Quick Reader");
		setSize(new Dimension(800, 600));
		setResizable(false);
		setDefaultCloseOperation(TextProcessor.close());
	}

	private void buildMenu() {
		menuBar = new JMenuBar();
		JMenu menu = new JMenu("Plik");
		JMenuItem openItem = new JMenuItem("Open");
		JMenuItem exitItem = new JMenuItem("Exit");

		openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
				ActionEvent.CTRL_MASK));
		openItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser fileChooser = new JFileChooser("./");
				int effect = fileChooser.showOpenDialog(null);
				if(effect == JFileChooser.APPROVE_OPTION){
					try {
						textProcessor.loadFile(fileChooser.getSelectedFile());
						wordPanel.update("Loaded: " +fileChooser.getSelectedFile().getName());
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

			}
		});

		exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
				ActionEvent.CTRL_MASK));
		exitItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				TextProcessor.close();
				System.exit(0);
			}
		});
		menu.add(openItem);
		menu.add(exitItem);
		menuBar.add(menu);
		setJMenuBar(menuBar);
	}

}
